package com.lq.order.controller;

import com.lq.common.result.Result;
import com.lq.order.entity.Order;
import com.lq.order.entity.dto.OrderDTO;
import com.lq.order.service.IOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author lq
 * @since 2025-08-22
 */
@RestController
@RequestMapping("/orders")
@Api(tags = "订单管理", description = "订单相关接口")
public class OrderController {

    @Autowired
    private IOrderService orderService;


    @ApiOperation(value = "创建订单", notes = "创建一个新的订单")
    @PostMapping("/")
    public Result createOrder(@ApiParam(value = "订单信息", required = true) @RequestBody OrderDTO orderDTO) {
        orderService.createOrder(orderDTO);
        return Result.success();
    }

    @ApiOperation(value = "根据ID获取订单", notes = "根据订单ID获取订单详细信息")
    @GetMapping("/{id}")
    public Result getOrderById(@ApiParam(value = "订单ID", required = true) @PathVariable Long id) {
        return Result.success(orderService.getById(id));
    }

    @ApiOperation(value = "获取所有订单", notes = "获取系统中所有的订单")
    @GetMapping("/")
    public Result getAllOrders() {
        return Result.success(orderService.list());
    }

    @ApiOperation(value = "更新订单", notes = "根据订单ID更新订单信息")
    @PutMapping("/{id}")
    public Result updateOrder(@ApiParam(value = "订单ID", required = true) @PathVariable Long id,
                             @ApiParam(value = "订单信息", required = true) @RequestBody Order order) {
        order.setId(id);
        orderService.updateById(order);
        return Result.success(order);
    }

    @ApiOperation(value = "删除订单", notes = "根据订单ID删除订单")
    @DeleteMapping("/{id}")
    public Result deleteOrder(@ApiParam(value = "订单ID", required = true) @PathVariable Long id) {
        orderService.removeById(id);
        return Result.success();
    }
}
